﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Miner.Model.Logic;
using Miner.UI.Forms.Interfaces;

namespace Miner.UI.Forms
{
    public partial class NewGameForm : Form, INewGameForm
    {
        private bool starting = false;
        
        public NewGameForm()
        {
            InitializeComponent();
            SetDefaults();
        }

        private void NewGameForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(!starting && this.Exiting!=null)
            {
                this.Exiting(this,EventArgs.Empty);
            }
        }

        private void bntStart_Click(object sender, EventArgs e)
        {
            starting = true;
            if(Starting!=null)
            {
                Starting(new GameOptions(numWidth.Value, numHeight.Value, numMines.Value));
            }
            this.Close();
        }

        public event EventHandler Exiting;

        public event StartGameDelegate Starting;

        private void btnClear_Click(object sender, EventArgs e)
        {
            SetDefaults();
        }

        private void SetDefaults()
        {
            numWidth.Value = 5;
            numHeight.Value = 5;
            numMines.Value = 1;

            numWidth.Focus();
        }


        public void Run()
        {
            this.ShowDialog();
        }


        public void Kill()
        {
            this.Dispose();
        }

        private void NewGameForm_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
    }
}
